#!/bin/bash
#
#********************************************************************
#Author:                songliangcheng
#QQ:                    2192383945
#Date:                  2023-2-16
#FileName：             install-dns.sh
#URL:                   blog.mykernel.cn
#Description：          一键生成dns curl -SsL https://gitee.com/slcnx/tools/raw/master/install-dns.sh | sed 's/\r//g' | bash -s -- --forwarders 114.114.114.114 --domain mycloud.com  --domain-net 10.68.19
#Copyright (C):        2023 All rights reserved
#********************************************************************

set -e
#描述:
#--forwarders 选项必须传递
#--forward 选项可以省略
#--dnssec-validation 选项可以省略
#--allow-query 选项可以省略
#--domain 选项可以省略
#--domain-net 选项可以省略

source <(curl -sSLf https://gitee.com/slcnx/tools/raw/master/parse_cmd.sh | sed 's/\r//g')

CONFIG='

key    ,argument    ,isempty    ,desc

--forwarders    ,forwarders    ,0    ,转发到哪

--forward    ,forward    ,1    ,转发规则 默认first/only

--dnssec-validation    ,dnssec_validation    ,1    ,默认no/auto

--allow-query    ,allow_query    ,1    ,默认所有允许查询 any/localhost

--domain    ,domain    ,1    ,默认mykernel.cn

--domain-net    ,domain_net    ,1    ,默认192.168.1

'

parse_cmd "$@"

echo $forwarders

: ${forward:=only}
: ${dnssec_validation:=no}
: ${allow_query:=any}
: ${domain:=mykernel.cn}
: ${domain_net:=192.168.1}

echo "$forward $dnssec_validation $allow_query $domain $domain_net "

result=$(echo "$domain_net" | xargs -d. -n1  | tac | sed '/^$/d' | tr '\n' '.' | sed 's/.$//')




apt install bind9 -y


cat > /etc/bind/named.conf.options <<EOF
options {
	directory "/var/cache/bind";

	forwarders {
	   ${forwarders};
	};
        forward ${forward};

	recursion yes;
        allow-query { ${allow_query}; };

	dnssec-validation ${dnssec_validation};
	listen-on-v6 { any; };
};
EOF


cat > /etc/bind/named.conf.local <<EOF
zone "${domain}" {
     type master;
     file "/etc/bind/db.${domain}";
};

zone "${result}.in-addr.arpa" {
     type master;
     file "/etc/bind/db.${domain_net}";
};
EOF
cat > /etc/bind/db.${domain} <<EOF
;
; BIND data file for dev sites
;
\$TTL    604800
@       IN      SOA     ${domain}. root.${domain}. (
                              1         ; Serial
                              1h         ; Refresh
                              1d         ; Retry
                              1w         ; Expire
                              1d )       ; Negative Cache TTL
;
@       IN      NS      ${domain}.
@       IN      A       ${domain_net}.134

*.${domain}.  14400   IN      A       ${domain_net}.134
EOF
cat > /etc/bind/db.${domain_net} <<EOF
;
; BIND reverse data file for dev domains
;
\$TTL    604800
@       IN      SOA     dev. root.dev. (
                              2         ; Serial
                              1h         ; Refresh
                              1d         ; Retry
                              1w         ; Expire
                              1d )       ; Negative Cache TTL
;
@        IN      NS      ${domain}.
134      IN      PTR     ${domain}.
134      IN      PTR     www.${domain}.
135      IN      PTR     blog.${domain}.
EOF


service bind9 restart

echo 'test.............'
dig -t A www.${domain} @127.0.0.1
dig -t PTR 134.${result}.in-addr.arpa @127.0.0.1
dig -t PTR 135.${result}.in-addr.arpa @127.0.0.1

echo
echo
echo
cat <<EOF
==============================================
dns服务已经完成安装!!

正向解析域为: ${domain}
反向解析域为: ${result}.in-addr.arpa
==============================================
EOF
